import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.ImageObserver;

/**
 * This class is just used to draw non-game elements to the screen.
 * @author Chris White
 *
 */
public class HUD {

	static final int LIVES = 3;
	// game variables
	static private int extraLives = LIVES-1;
	static private int score = 0;
	static private int gameOverTime = 0;
	static private final int GAMEOVERWAIT = 100;
	static private boolean gameIsOver;
	static private boolean resetMe;

	static public void resetGame(){
		extraLives  = LIVES;
		score = 0;
		gameOverTime = 0;
		gameIsOver = false;
		resetMe = false;
	}
	
	static public Boolean resetMe(){
		return resetMe;
	}
	
	static public int readScore(){
		return score;
	}

	static public void modifyScore(int points){
		score += points;
	}
	
	static public int readLives(){
		return extraLives;
	}
	
	static public void modifyLives(int change){
		extraLives += change;
	}
	
	static public void gameOver(){
		gameIsOver = true;
	}
	
	/**
	 * Draws the number provided, started from the given location, using the digits in SpriteLib.NUMBERS
	 * 
	 */
	static void drawNumber(Graphics g, ImageObserver io, int x, int y, int num){
		
		int i = 1; // always print at least one digit, even if it is a zero
		
		// set i equal to the number of digits in our number - curious if this could be faster than the other way... maybe?
//		i = (""+num).length();
		
		// set i equal to the number of digits in our number
		while(Math.floor((double)num / Math.pow(10, i)) > 0.0){
			i++;
		}
		
		// now draw the character for each number
		while(i > 0){
			//floor((num % 10^n)/ 10^(n-1)) is the majick formula to pull out the nth digit of num.
			// I pull the nth digit from the number and use it as the image index for SpriteLib.NUMBERS
			g.drawImage(SpriteLib.NUMBERS.getFrame((int)Math.floor(num % Math.pow(10, i) / Math.pow(10, i-1))), x, y, io);
			i--;
			x+=16;
		}
	}

	static void draw(Graphics g, ImageObserver io){
		// Draw score
		g.drawImage(SpriteLib.SCORE.getFrame(0), 10, 10, io);
		drawNumber(g, io, 10, 34, score);
		
		// Draw lives
		int x = 260;
		g.drawImage(SpriteLib.LIVES.getFrame(0), x, 10, io);
		for(int i = 0; i < extraLives; i++){
			g.drawImage(SpriteLib.DEFENDER.getFrame(0), x, 34, io);
			x+=SpriteLib.DEFENDER.image_width*1.25;
		}
		
		if(gameIsOver){
			g.drawImage(SpriteLib.GAME_OVER.getFrame(0), 320 - SpriteLib.GAME_OVER.image_width/2, 240 - SpriteLib.GAME_OVER.image_height/2, io);
			if(gameOverTime >= GAMEOVERWAIT){
				resetMe = true;
			}
			gameOverTime += 1;
		}
	}
}
